import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import dts from 'vite-plugin-dts'
import terser from '@rollup/plugin-terser'
import viteCompression from 'vite-plugin-compression'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    viteCompression({
      verbose: false,
      disable: false,
      threshold: 10240,
      algorithm: 'gzip',
      ext: '.gz',
    }),
    dts({
      rollupTypes: true,
      include: ['src/components/**/*.vue', 'src/components/**/*.ts'],
      beforeWriteFile: (filePath, content) => {
        return {
          filePath: filePath.replace('src/components/', ''),
          content
        }
      }
    })
  ],
  build: {
    outDir: 'dist',
    minify: 'terser',
    sourcemap: false,
    reportCompressedSize: false,
    chunkSizeWarningLimit: 1000,
    terserOptions: {
      compress: {
        drop_console: true,
        drop_debugger: true
      }
    },
    lib: {
      entry: process.env.COMPONENT_NAME 
        ? resolve(__dirname, `src/components/${process.env.COMPONENT_NAME}/index.vue`)
        : resolve(__dirname, 'src/components/index.ts'),
      name: process.env.COMPONENT_NAME || 'WMCComponents',
      fileName: (format) => `index.${format === 'es' ? 'mjs' : 'js'}`
    },
    rollupOptions: {
      external: ['vue', 'element-plus'],
      output: {
        globals: {
          vue: 'Vue',
          'element-plus': 'ElementPlus'
        }
      }
    }
  },
  optimizeDeps: {
    exclude: ['vue', 'element-plus']
  },
  resolve: {
    alias: {
      '@': resolve(__dirname, 'src')
    }
  },
  logLevel: 'error'
}) 